from absl import app, flags
import subprocess
import os
import shlex
import difflib
import time


def main(_):
  trading_date = flags.FLAGS.trading_date  # "20190901"
  time_range = flags.FLAGS.time_range  # "0-0.5"
  parsers = flags.FLAGS.compare.split(",")

  setting_dict = {
      'slow': ("slow__", "RUST_TOPIC_RECORD=1", "slow", "False"),
      'rust': ("rust__", "RUST_TOPIC_RECORD=1 RUST_FEED_HANDLER=1", "rust", "False"),
      'feed_cache': ("fcache", "", "feed_cache", "True"),
  }

  if flags.FLAGS.out is not None:
    outfile = open(flags.FLAGS.out, 'w')
  else:
    outfile = None
  if True:
    for exchange, symbol, market_type in [
        ("Huobi", "BTC-USDT", "Spot"),
        ("Bitmex", "BTC-USD.PERPETUAL", "Futures"),
        ("Okex", "BTC-USD.QUARTER", "Futures"),
        ("Huobi", "BTC-USD.QUARTER", "Futures"),
    ]:
      prev_stdout = None
      prev_name = None

      for name, envexport, postfix, use_feed_cache in [setting_dict[parser] for parser in parsers]:
        # if exchange == "Huobi" and market_type == "Spot" and use_feed_cache != "True":
        #  worker_id = "1"
        # else:
        #  worker_id = "1,2"
        worker_id = "1,2"
        cmd = (f"{envexport} ./pyrunner coin/strategy/mm/tool/feed_archive_checker.py"
               + f" --postfix={postfix} --trading_date={trading_date}"
               + f" --exchange={exchange} --symbols={symbol}"
               + f" --time_range={time_range} --use_feed_cache={use_feed_cache}"
               + f" --market_type={market_type} --worker_ids={worker_id}"
               + f" --feed_machine=feed-01.cn-hongkong.aliyun --print_all_feed --out_dir=''")
        print(cmd)
        cmdargs = shlex.split(cmd)

        prev_time = time.time()

        result = subprocess.check_output(cmd, shell=True)
        stdout = result.decode("utf-8")
        if False:
          with open(os.devnull, 'w') as devnull:
            result = subprocess.run(cmdargs, stdout=subprocess.PIPE, stderr=devnull)
          stdout = result.stdout.decode("utf-8")

        num_rows = stdout.count('\n')

        print(f"{trading_date} {name} {exchange} {symbol} {time_range} "
              + f"elapsed time {time.time() - prev_time : 8.2f} num rows: {num_rows}",
              file=outfile,
              flush=True)
        if prev_stdout is not None:
          diff_ratio = difflib.SequenceMatcher(None, prev_stdout, stdout).quick_ratio()
          print(f"{trading_date} {name} {exchange} {symbol} "
                + f"diff ratio {prev_name} vs {name}: {diff_ratio: .3f}",
                file=outfile,
                flush=True)
        prev_stdout = stdout
        prev_name = name


if __name__ == '__main__':
  flags.DEFINE_string('trading_date', None, 'trading_date')
  flags.DEFINE_string('time_range', None, 'time_range')
  flags.DEFINE_string('compare', None, 'compare')
  flags.DEFINE_string('out', None, 'out')
  app.run(main)
